//
//  HWExchangeViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/13.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWExchangeViewController.h"
#import "HWExchangeduihuanCell.h"
#import "HWxuanzedizhiTableViewCell.h"
#import "HWAddressManagementController.h"
#import "HWSetupModel.h"


@interface HWExchangeViewController ()<UITableViewDataSource,UITableViewDelegate,HWAddressManagementDelegate>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic) HWMallGoogsAddressModel *seleeModel;
@property(strong,nonatomic) UIView *lowerPartView;
@end

@implementation HWExchangeViewController

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = RGB(232, 232, 242);
        
    }
    return   _mainaTableVie ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    
    
    
}

- (void)setupUI {
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"立即兑换";
    [self addkongjianView];
    
}

// 添加控件
- (void)addkongjianView {
    
    
    [self.view addSubview:self.mainaTableVie];
    
    [self addLowerPartView];
    
    
}



- (void)addLowerPartView {
    
    self.lowerPartView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenH-44-CF_TabbarSafeBottomMargin, kScreenW, 44+CF_TabbarSafeBottomMargin)];
    // self.lowerPartView.backgroundColor = [UIColor yellowColor];
    self.lowerPartView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.lowerPartView];
    
    UIButton *butt  = [UIButton setButtonWithFrame:CGRectMake(35, 7, kScreenW-35*2, 30) Title:@"立即兑换" Image:nil Font:13 Color:RGB(255, 255, 155) Target:self Selector:@selector(buttonTap:)];
    
    butt.backgroundColor = RGB(38, 154, 255);
    butt.layer.cornerRadius = 15;
    butt.layer.masksToBounds = YES;
    
    butt.backgroundColor = [UIColor bm_colorGradientChangeWithSizeView:butt];
    
    [self.lowerPartView addSubview:butt];
    
    
    
    
    
}

- (void)buttonTap:(UIButton *)sender {
    
    NSLog(@"ijnccijphds");
    if ([self.dataDic[@"cat_id"] isEqualToString:@"3"]) {
    }else {
        if (self.seleeModel == nil) {
            [MBProgressHUD showError:@"请选择地址"];
            return;
            
        }
    }
    
    
    int  r =  [WMUtil getRandomNumber:101 to:998];
    
    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],r];
    
    
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];
    
    
    NSDictionary *parametes = @{
        
        @"ios" : @"1",
        @"ct" :  @"app",
        @"ac" : @"mall_buy",
        @"uid" :uidStr,
        @"t" : tStr,
        @"sign" : singMD5,
        @"id"  : self.dataDic[@"goods_id"],
        @"address_id": self.seleeModel.Id ? self.seleeModel.Id : @"",
        @"num" : @"1"
    };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        if ([codeStr intValue] == 1) {
            
            [MBProgressHUD showSuccess:response[@"message"]];
            
            [self.navigationController popViewControllerAnimated:YES];
            
            
            
        } else  {
            
            [MBProgressHUD showError:response[@"meaages"]];
            
        }
        
        
        
    } failure:^(NSString *error) {
        [MBProgressHUD showError:@"兑换失败"];
        
        
    }];
    
    
    
    
    
    
    
}



//-(int)getRandomNumber:(int)from to:(int)to
//{
//   return (int)(from + (arc4random() % (to - from + 1)));
//}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if ([self.dataDic[@"cat_id"] isEqualToString:@"3"]) {
        return  1;
    }
    return  2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return  1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.dataDic[@"cat_id"] isEqualToString:@"3"]) {
        static NSString *cellID = @"cellID";
        HWExchangeduihuanCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HWExchangeduihuanCell" owner:nil options:nil].lastObject;
            
        }
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        [cell.iconIamge sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"goods_img"]] placeholderImage:nil];
        
        
        
        cell.name1Label.text = [NSString stringWithFormat:@"%@", self.dataDic[@"goods_name"]];
        
        cell.name2label.text = [NSString stringWithFormat:@"积分：%@",self.dataDic[@"market_price"]];
        cell.name3Label.text = [NSString stringWithFormat:@"数量：%@",@"1"];
        
        cell.name1Label.textColor = RGB(51, 51, 51);
        cell.name2label.textColor = RGB(51, 51, 51);
        cell.name3Label.textColor = RGB(51, 51, 51);
        
        return  cell;
    }
    
    if (indexPath.section == 0) {
        
        static NSString *cellID1 = @"cellID1";
        HWxuanzedizhiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID1];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HWxuanzedizhiTableViewCell" owner:nil options:nil].lastObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        if (self.seleeModel == nil) {
            cell.xuanzeLabel.hidden = NO;
            cell.name1Label.hidden = YES;
            cell.dianhuaLabel.hidden = YES;
            cell.dizhilabel.hidden = YES;
        } else {
            
            cell.xuanzeLabel.hidden = YES;
            cell.name1Label.hidden = NO;
            cell.dianhuaLabel.hidden = NO;
            cell.dizhilabel.hidden = NO;
            
            
            cell.name1Label.text = self.seleeModel.consignee;
            cell.dianhuaLabel.text = self.seleeModel.mobile;
            cell.dizhilabel.text = [NSString stringWithFormat:@"%@", self.seleeModel.uaddress];
            
            
            
            
        }
        
        cell.name1Label.textColor = RGB(51, 51, 51);
        cell.dianhuaLabel.textColor = RGB(51, 51, 51);
        cell.dizhilabel.textColor = RGB(51, 51, 51);
        cell.xuanzeLabel.textColor = RGB(51, 51, 51);
        
        return  cell;
        
        
        
    } else {
        
        
        static NSString *cellID = @"cellID";
        HWExchangeduihuanCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HWExchangeduihuanCell" owner:nil options:nil].lastObject;
            
        }
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        
        [cell.iconIamge sd_setImageWithURL:[NSURL URLWithString:self.dataDic[@"goods_img"]] placeholderImage:nil];
        
        
        
        cell.name1Label.text = [NSString stringWithFormat:@"%@", self.dataDic[@"goods_name"]];
        
        cell.name2label.text = [NSString stringWithFormat:@"积分：%@",self.dataDic[@"market_price"]];
        cell.name3Label.text = [NSString stringWithFormat:@"数量：%@",@"1"];
        
        cell.name1Label.textColor = RGB(51, 51, 51);
        cell.name2label.textColor = RGB(51, 51, 51);
        cell.name3Label.textColor = RGB(51, 51, 51);
        
        return  cell;
        
        
        
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.dataDic[@"cat_id"] isEqualToString:@"3"]) {
        return  120;
    }
    if (indexPath.section == 0) {
        return  70;
    }
    return  120;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if ([self.dataDic[@"cat_id"] isEqualToString:@"3"]) {
        return  0;
    }
    return  10;
    
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 10)];
    view.backgroundColor = RGB(232, 232, 242);
    return  view;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.dataDic[@"cat_id"] isEqualToString:@"3"]) {
        return;
    }
    if (indexPath.section == 0) {
        
        HWAddressManagementController *address = [[HWAddressManagementController alloc]init];
        address.panduanStr = @"1";
        address.delegate = self;
        [self.navigationController pushViewController:address animated:YES];
        
    } else {
        
        
        
        
    }
    
    
}



// 选中的
- (void)xuanzhongModel:(HWMallGoogsAddressModel *)model {
    
    self.seleeModel = model;
    
    [self.mainaTableVie reloadData];
    
    
    
    
}







@end
